<template>
    <div ref="chartContainer" class="chart"></div>
  </template>
  
  <script setup>
  import { ref, onMounted, onUnmounted } from 'vue';
  import * as echarts from 'echarts';
  
  // 定义图表 DOM 容器的 ref
  const chartContainer = ref(null);
  let chartInstance = null;
  
  // 定义图表配置和数据
  const option = {
    title: {
      text: '各科目成绩对比'
    },
    legend: {
      data: ['平均分', '用户分数', '一本线']
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    dataset: {
      source: [
        ['科目', '平均分', '用户分数', '一本线'],
        ['语文', 85, 140, 88],
        ['数学', 82, 87, 85],
        ['英语', 78, 85, 80],
        ['物理', 75, 80, 78],
        ['化学', 79, 84, 81],
        ['生物', 76, 82, 79],
        ['历史', 70, 75, 72],
        ['地理', 74, 78, 75],
        ['政治', 72, 77, 74]
      ]
    },
    xAxis: {
      type: 'category'
    },
    yAxis: {
      type: 'value'
    },
    series: [
      { name: '平均分', type: 'bar' },
      { name: '用户分数', type: 'bar' },
      { name: '一本线', type: 'bar' }
    ]
  };
  
  // 组件挂载后初始化图表
  onMounted(() => {
    if (chartContainer.value) {
      chartInstance = echarts.init(chartContainer.value);
      chartInstance.setOption(option);
    }
  });
  
  // 组件卸载前销毁图表实例
  onUnmounted(() => {
    if (chartInstance) {
      chartInstance.dispose();
    }
  });
  </script>
  
  <style scoped>
  .chart {
    width: 100%;
    height: 300px;
  }
  </style>
  